/**
 * 
 */
package com.android.hw.projectmenu;


import com.qualcomm.qcnvitems.QcNvItems;
import com.android.hw.projectmenu.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * @author gaoshengpan
 *
 */
public class MMITestResult extends ListActivity {
	private String[] mTestResultString;
	private QcNvItems mNv;

	Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
         setResultInfo();
        }
    };
	/**
	 * 
	 */
	public MMITestResult() {
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mmi_result);
		mNv = new QcNvItems(this);

	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mNv == null) {
            mNv = new QcNvItems(this);
        }
		mHandler.sendEmptyMessageDelayed(1, 2000);
		
	}

	private void setResultInfo() {
		mTestResultString = new String[2];

		mTestResultString[0] = getString(R.string.mmi1_test) + "       "
				+ getResultDetails(3);
		mTestResultString[1] = getString(R.string.mmi2_test) + "       "
				+ getResultDetails(14);
		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mTestResultString));
	}
	
	/* (non-Javadoc)
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent();
		switch (position) {
		case 0:
			intent.setClass(this, MMITestResultDetails.class);
			intent.putExtra("position", 0);
			startActivity(intent);
			break;
		case 1:
			intent.setClass(this, MMITestResultDetails.class);
			intent.putExtra("position", 1);
			startActivity(intent);
			break;
		
		default:
			break;
		}
	}
	
	private String getResultDetails(int i) {
		String NV2499 = "";
		String mmitestresult = "";
		
        byte[] bresult = null;
		if (mNv != null) {
			try {
				bresult = mNv.getNvFactoryData3IByte();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}

        if (bresult == null) {
            return mmitestresult;
        } 
       /* 15-09-22 gaoshenpan fix show null when nv not write begin*/
        mmitestresult = (char)bresult[i] == 'P' ? getString(R.string.success) : ((char)bresult[i] == 'F' ? getString(R.string.fail) : "");
        /* 15-09-22 gaoshenpan fix show null when nv not write end*/
		return mmitestresult;
	}
	
	/* (non-Javadoc)
	 * @see android.app.ListActivity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mNv != null) {
            mNv.dispose();
        }
	}
	
	
}
